package cn.lizemin.condition;

import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.io.Resource;
import org.springframework.core.type.AnnotatedTypeMetadata;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * @author lzm
 * @date 2025/10/1 10:41
 * @description 判断Cat实例是否注入到spring容器中
 */
public class CatCondition implements Condition {

    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        Resource resource = context.getResourceLoader().getResource("classpath:condition.properties");
        if (resource.exists()) {
            Properties properties = new Properties();
            try (InputStream inputStream = resource.getInputStream()) {
                properties.load(inputStream);
                String property = properties.getProperty("cat.enabled");
                return Boolean.parseBoolean(property);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return false;
    }

}
